package com.tensquare.recruit.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.tensquare.recruit.pojo.Enterprise;
import com.tensquare.recruit.service.EnterpriseService;
import entity.Result;
import entity.StatusCode;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @Description 招聘企业模块
 * @Date 2021/3/19 13:22
 */
@RestController
@CrossOrigin
@RequestMapping("/enterprise")
public class EnterpriseController {

    @Autowired
    private EnterpriseService enterpriseService;


    /***
     * 增加企业
     * Param: [label]
     **/
    @PostMapping()
    public Result save(@RequestBody Enterprise enterprise) {
        enterpriseService.save(enterprise);
        return new Result(true, StatusCode.OK, "保存成功");
    }

    /***
     * 查询所有企业
     * Param: []
     **/
    @GetMapping
    public Result findAll() {
        return new Result(true, StatusCode.OK, "查询成功", enterpriseService.findAll());
    }

    /***
     * 根据企业id查询企业
     * Param: [enterpriseId]
     **/
    @GetMapping("/{enterpriseId}")
    public Result findById(@PathVariable String enterpriseId) {
        return new Result(true, StatusCode.OK, "查询成功", enterpriseService.findById(enterpriseId));
    }

    /***
     * 修改企业信息
     * Param: [enterprise, enterpriseId]
     **/
    @PutMapping("/{enterpriseId}")
    public Result save(@RequestBody Enterprise enterprise, @PathVariable String enterpriseId) {
        enterprise.setId(enterpriseId);
        enterpriseService.update(enterprise);
        return new Result(true, StatusCode.OK, "更新成功");
    }


    /***
     * 根据id删除企业
     * Param: [enterpriseId]
     **/
    @DeleteMapping("/{enterpriseId}")
    public Result deleteById(@PathVariable String enterpriseId) {
        enterpriseService.deleteById(enterpriseId);
        return new Result(true, StatusCode.OK, "删除成功");
    }

    /***
     * 条件查询
     * Param: [enterprise]
     **/
    @PostMapping("/search")
    public Result findSearch(@RequestBody Enterprise enterprise) {
        List<Enterprise> enterpriseList = enterpriseService.findSearch(enterprise);
        return new Result(true, StatusCode.OK, "条件查询成功", enterpriseList);
    }

    /***
     * 查询热门企业列表
     * Param: []
     **/
    @GetMapping("/search/hotlist")
    public Result hotList() {
        return new Result(true, StatusCode.OK, "查询成功", enterpriseService.findByIshot());
    }

    /***
     * 分页条件查询企业列表
     * Param: [enterprise, page, size]
     **/
    @PostMapping("/search/{page}/{size}")
    public Result findSearch(@RequestBody Enterprise enterprise, @PathVariable int page, @PathVariable int size) {
        Page<Enterprise> enterpriseList = enterpriseService.findSearchByPage(enterprise, page, size);
        return new Result(true, StatusCode.OK, "条件查询成功", enterpriseList);
    }

}
